package tpase.StorageServer.DAO;

import tpase.StorageServer.DO.FileInfo;
import tpase.StorageServer.DO.UserAuth;
import util.JDBCUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileInfoDao {
    private String configPath;
    public FileInfoDao(String configPath) {
        this.configPath = configPath;
    }

    public void clearFileInfo() {
        Connection connection = JDBCUtil.getConnect(configPath);
        String sql = "DELETE FROM file";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareCall(sql);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement);
        }
    }

    public void storeFileInfo(FileInfo  fileInfo) {
        Connection connection = JDBCUtil.getConnect(configPath);
        String sql = "INSERT INTO file (fid, file_name, path, size, type, create_time, uid) VALUES (?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, fileInfo.getFid());
            statement.setString(2, fileInfo.getFileName());
            statement.setString(3, fileInfo.getPath());
            statement.setLong(4, fileInfo.getSize());
            statement.setString(5, fileInfo.getType());
            statement.setTimestamp(6, fileInfo.getCreateTime());
            statement.setString(7, fileInfo.getUid());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement);
        }
    }

    /**
     * 查询文件
     * @param uid
     * @return
     */
    public List<FileInfo> getFileInfoList(String uid) {
        Connection connection = JDBCUtil.getConnect(configPath);
        String sql = "SELECT fid, file_name, path, size, type, create_time, uid FROM file WHERE uid = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<FileInfo> fileInfoList = new ArrayList<>();
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1, uid);
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                fileInfoList.add(new FileInfo(resultSet.getString("fid"),
                        resultSet.getString("file_name"),
                        resultSet.getString("path"),
                        resultSet.getLong("size"),
                        resultSet.getString("type"),
                        resultSet.getTimestamp("create_time"),
                        resultSet.getString("uid")));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return fileInfoList;
    }

    public FileInfo getFileInfo(String fid) {
        Connection connection = JDBCUtil.getConnect(configPath);
        String sql = "SELECT fid, file_name, path, size, type, create_time, uid FROM file WHERE fid = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        FileInfo fileInfo = null;
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1, fid);
            resultSet = statement.executeQuery();

            if (resultSet.next()) {
                fileInfo = new FileInfo(resultSet.getString("fid"),
                        resultSet.getString("file_name"),
                        resultSet.getString("path"),
                        resultSet.getLong("size"),
                        resultSet.getString("type"),
                        resultSet.getTimestamp("create_time"),
                        resultSet.getString("uid"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return fileInfo;
    }


    public void deleteFileInfo(String fid) {
        Connection connection = JDBCUtil.getConnect(configPath);
        String sql = "DELETE FROM file WHERE fid = ?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1, fid);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement);
        }
    }

    public static void main(String[] args) {
        FileInfoDao fileInfoDao = new FileInfoDao("config/config.properties");
        fileInfoDao.storeFileInfo(new FileInfo("1", "1.txt", "src/main/resources/files/t1/1.txt", 1024L, "txt", new Timestamp(System.currentTimeMillis()), "t1"));
//        fileInfoDao.storeFileInfo(new FileInfo("2", "filename2", "path\test", 1024L, "pdf", new Timestamp(System.currentTimeMillis()), "t1"));
//        fileInfoDao.storeFileInfo(new FileInfo("3", "filename2", "path\test", 1024L, "pdf", new Timestamp(System.currentTimeMillis()), "t1"));
//        fileInfoDao.storeFileInfo(new FileInfo("4", "filename2", "path\test", 1024L, "pdf", new Timestamp(System.currentTimeMillis()), "t1"));
//        fileInfoDao.storeFileInfo(new FileInfo("5", "filename2", "path\test", 1024L, "pdf", new Timestamp(System.currentTimeMillis()), "t1"));
//        fileInfoDao.deleteFileInfo("2");


//        System.out.println(fileInfoDao.getFileInfo("1"));

//        for (FileInfo fileInfo : fileInfoDao.getFileInfoList("t1")) {
//            System.out.println(fileInfo);
//        }

//        fileInfoDao.clearFileInfo();
    }
}
